/*
 *  Copyright 2019-2023 Zheng Jie
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package me.zhengjie.stock.rest;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import me.zhengjie.annotation.Log;
import me.zhengjie.stock.domain.TblTradingDateRecord;
import me.zhengjie.stock.domain.vo.TblTradingDateRecordQueryCriteria;
import me.zhengjie.stock.service.TblTradingDateRecordService;
import me.zhengjie.utils.PageResult;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author dolojia
 * @date 2024-05-04
 **/
@RestController
@RequiredArgsConstructor
@Api(tags = "交易日期记录管理")
@RequestMapping("/api/tblTradingDateRecord")
public class TblTradingDateRecordController {

    private final TblTradingDateRecordService tblTradingDateRecordService;

    @Log("导出交易日期记录数据")
    @ApiOperation("导出交易日期记录数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('tblTradingDateRecord:list')")
    public void exportTblTradingDateRecord(HttpServletResponse response, TblTradingDateRecordQueryCriteria criteria) throws IOException {
        tblTradingDateRecordService.download(tblTradingDateRecordService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询交易日期记录")
    @ApiOperation("查询交易日期记录")
    @PreAuthorize("@el.check('tblTradingDateRecord:list')")
    public ResponseEntity<PageResult<TblTradingDateRecord>> queryTblTradingDateRecord(TblTradingDateRecordQueryCriteria criteria, Page<Object> page) {
        return new ResponseEntity<>(tblTradingDateRecordService.queryAll(criteria, page), HttpStatus.OK);
    }

    @PostMapping
    @Log("新增交易日期记录")
    @ApiOperation("新增交易日期记录")
    @PreAuthorize("@el.check('tblTradingDateRecord:add')")
    public ResponseEntity<Object> createTblTradingDateRecord(@Validated @RequestBody TblTradingDateRecord resources) {
        tblTradingDateRecordService.create(resources);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改交易日期记录")
    @ApiOperation("修改交易日期记录")
    @PreAuthorize("@el.check('tblTradingDateRecord:edit')")
    public ResponseEntity<Object> updateTblTradingDateRecord(@Validated @RequestBody TblTradingDateRecord resources) {
        tblTradingDateRecordService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除交易日期记录")
    @ApiOperation("删除交易日期记录")
    @PreAuthorize("@el.check('tblTradingDateRecord:del')")
    public ResponseEntity<Object> deleteTblTradingDateRecord(@RequestBody List<Integer> ids) {
        tblTradingDateRecordService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @GetMapping(value = "/queryLastTradingDateRecord")
    @Log("查询最后的交易日期")
    @ApiOperation("查询最后的交易日期")
    @PreAuthorize("@el.check('tblTradingDateRecord:list')")
    public ResponseEntity<Object> queryLastTradingDateRecord() {
        LambdaQueryWrapper<TblTradingDateRecord> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByDesc(TblTradingDateRecord::getTradingDate);
        queryWrapper.last("limit 1");
        TblTradingDateRecord tblTradingDateRecord = tblTradingDateRecordService.getOne(queryWrapper);
        return new ResponseEntity<>(tblTradingDateRecord, HttpStatus.OK);
    }

}